package com.atguigu.base;

import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/9/23 14:05
 * @Description:
 */
public abstract class BaseServiceImpl<T> {

    public abstract BaseMapper<T> getBaseMapper();

    /**
     * @Description 查询所有信息
     * @return: java.util.List<com.atguigu.entity.Role>
     */
    public List<T> findAll() {
        return getBaseMapper().findAll();
    }

    /**
     * @Description 添加
     * @param t:
     * @return: void
     */
    public void insert(T t) {
        getBaseMapper().insert(t);
    }

    /**
     * @Description 根据id查询信息
     * @param id:
     * @return: com.atguigu.entity.Role
     */
    public T getById(Long id) {
        return getBaseMapper().getById(id);
    }

    /**
     * @Description 修改信息
     * @param t:
     * @return: void
     */
    public void update(T t) {
        getBaseMapper().update(t);
    }

    /**
     * @Description 删除信息
     * @param id:
     * @return: void
     */
    public void delete(Long id) {
        getBaseMapper().delete(id);
    }

    /**
     * @Description 分页查询信息
     * @param filters:
     * @return: com.github.pagehelper.PageInfo<com.atguigu.entity.Role>
     */
    public PageInfo<T> findByPage(Map<String, Object> filters) {
        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"), 5);
        PageHelper.startPage(pageNum, pageSize);
        Page<T> page = getBaseMapper().findByPage(filters);
        return new PageInfo<>(page,10);
    }
}
